<?php
//操作员类
class operator {

	var $user;		//用户名
	var $pwd;		//密码
	
	/**
	  * logincheck
	  * 验证用户名是否可用
	  * @param $user 
	  * @param $pwd
	  * @return 1验证通过，2帐号停用，3密码错误，4帐号不存在
	  */
	function logincheck($user,$pwd) {
		global $DB;
		$sql = "SELECT mi_id, mi_user, mi_password, mi_name, mi_type, mi_status FROM ManageInfo WHERE mi_user='".$user."'";	
		$rs = $DB->Execute($sql);
		if($rs->RecordCount() > 0) {
			if($rs->fields["mi_password"] == $pwd) {
				if($rs->fields["mi_status"] == "0"){
					//设置SESSION
					$_SESSION["OP_ID"] 		= $rs->fields["mi_id"];
					$_SESSION["OP_USER"] 	= $rs->fields["mi_user"];
					$_SESSION["OP_NAME"]	= $rs->fields["mi_name"];
					$_SESSION["OP_TYPE"] 	= $rs->fields["mi_type"];
					$DB->Execute("UPDATE ManageInfo SET mi_date_login=NOW(), mi_ip_login='".getenv("REMOTE_ADDR")."' WHERE mi_id='".$rs->fields["mi_id"]."'");				
					return 1;
				}else {
					return 2;	
				} 
			}else {
				return 3;
			}	
		}else{
			return 4;
		}
	}
	
	/**
	 * logout
	 * 管理登出函数
	 */	
	function logout() {
		session_unregister("OP_ID");
		session_unregister("OP_USER");
		session_unregister("OP_NAME");
		session_unregister("OP_TYPE");
	}
	
	
	/**
	  * chkuser
	  * 验证用户名是否可用
	  * @param string $user 用户名字
	  * @return boolean if valid, return ture, else return false
	  */
	function chkuser($user) {
		global $DB;
		$sql = "SELECT COUNT(*) AS num FROM ManageInfo WHERE mi_user='".$user."'";
		$rs = $DB->Execute($sql);
		if(intval($rs->fields["num"]) > 0) return false;
		return true;
	}

	
	/**
	  * add_operator
	  * 新加操作员
	  * @param strint $name
	  */
	function add_operator($user, $pwd, $name, $type, $email, $status, $code){
	  global $DB,$GLOBAL;
	 
	  $sql = "INSERT INTO ManageInfo (mi_code, mi_user, mi_password, mi_name, mi_type, mi_email, mi_status, mi_date_in, mi_ip_in,mi_op_id) VALUES ('" . $code . "','" . $user . "', '" . md5($pwd) . "', '" . $name . "', '" . $type . "', '" . $email . "', '" . $status . "', NOW(),'" . getenv("REMOTE_ADDR") . "','" . $GLOBAL["OP_ID"] . "')";
	  $DB->Execute($sql);
	}

	/**
	  * list_operator
	  * 操作员列表
	  */
	function list_operator(){
	  global $DB;
	  $sql = "SELECT * FROM ManageInfo";
	  return $DB->Execute($sql);
	}
	
	/**
	  * get_operator_list
	  * 操作员列表
	  * @param char $type
	  */
	function get_optype_list($type){
	  global $DB;
	  $sql = "SELECT * FROM ManageInfo WHERE mi_type='".$type."' AND mi_status='A'";
	  return $DB->Execute($sql);
	}	
	/**
	  * operator_del
	  * 删除操作员
	  */
	function operator_del($op_id){
	  global $DB;
	  $sql = "DELETE FROM ManageInfo WHERE mi_id='".$op_id."'";
	  return $DB->Execute($sql);
	}
	
	/**
	  * set_opstatus
	  * 更改操作员状态
	  * @param string $type:0停用，1开通
	  * @param int $op_id 
	  */
	function set_opstatus($type, $op_id){
	  global $DB,$GLOBAL;
	  $sql = "UPDATE ManageInfo SET mi_status='".$type."',mi_date_mt=NOW(),mi_ip_mt='".getenv("REMOTE_ADDR")."', mi_op_id='".$GLOBAL["OP_ID"]."' WHERE mi_id='".$op_id."'";
	  return $DB->Execute($sql);
	}	
	
	/**
	  * operator_chgpwd
	  * 更改操作员密码
	  * @param string $pass
	  * @param int $op_id 
	  */
	function operator_chgpwd($opid, $pass){
	  global $DB,$GLOBAL;
	  $sql = "UPDATE ManageInfo SET mi_password='".md5($pass)."',mi_date_mt=NOW(),mi_ip_mt='".getenv("REMOTE_ADDR")."', mi_op_id='".$GLOBAL["OP_ID"]."' WHERE mi_id='".$opid."'";
	  return $DB->Execute($sql);
	}			
}
?>